package com.cocotingo.snail.samples;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JFrame;

import com.cocotingo.snail.MouseEvent;
import com.cocotingo.snail.ViewContext;
import com.cocotingo.snail.template.TemplateLoader;
import com.cocotingo.snail.template.TemplateParserException;
import com.cocotingo.snail.template.TemplateSemanticException;
import com.cocotingo.snail.template.TemplateSyntaxException;
import com.cocotingo.snail.template.TemplateView;
import com.cocotingo.snail.template.XMLTemplateParser;
import com.cocotingo.snail.template.XMLTemplateWritter;
import com.cocotingo.snail.template.annotations.TemplateCallback;
import com.cocotingo.snail.template.annotations.TemplateVariable;
import com.cocotingo.snail.template.models.TemplateDefinitionsNode;

public class SampleMain4 {

	public static void main(String[] args) {
		JFrame mainFrame = new JFrame("View Maker Tester");
		mainFrame.setSize(800, 600);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		ViewContext context = new ViewContext(mainFrame.getContentPane());
		
		TemplateLoader loader = new TemplateLoader();
		try {
//			File file = new File("output.txt");
//			final FileInputStream stream = new FileInputStream(file);
//			loader.load("XML", new ResourceLoader() {
//
//				@Override
//				public InputStream loadResourceByName(String name) {
//					if (name == "XML") return stream;
//					else return SampleMain4.class.getResourceAsStream(name);
//				}});
			loader.load("SampleUI.xml", SampleMain4.class);
		} catch (TemplateParserException e) {
			e.printStackTrace();
			return;
		} catch (TemplateSyntaxException e) {
			e.printStackTrace();
			return;
		} catch (TemplateSemanticException e) {
			e.printStackTrace();
			return;
		}
		
		TemplateView rootView = loader.makeView("SampleView3");
		
		context.setRootView(rootView);
		context.prepareContent();
		
		mainFrame.setVisible(true);
	}
	
	public static class LayoutSampleView extends TemplateView {
		
		@TemplateVariable public int listWidth = 0;
		@TemplateVariable public int btnHeight = 0;
		@TemplateVariable public int margin = 0;
		
		@TemplateCallback
		public void dragged(TemplateView sender, MouseEvent event) {
			listWidth = sender.getLeft() + event.getPosition(sender).x;
			layout();
		}
		
		@TemplateCallback
		public void btnOKClicked(TemplateView sender, MouseEvent event) {
			XMLTemplateParser parser = new XMLTemplateParser();
			XMLTemplateWritter writter = new XMLTemplateWritter();
			try {
				TemplateDefinitionsNode node = parser.parse(SampleMain4.class.getResourceAsStream("SampleUI.xml"));
				File file = new File("output.txt");
				FileOutputStream stream = new FileOutputStream(file);
				writter.write(node, stream);
				stream.close();
			} catch (TemplateParserException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static class MainView extends TemplateView {
		
		@TemplateVariable public TemplateView rect1 = null;
		@TemplateVariable public TemplateView rect2 = null;
		@TemplateVariable public TemplateView rect3 = null;
		@TemplateVariable public TemplateView rect4 = null;
		@TemplateVariable public int margin1 = 0;
		
		@Override
		protected void madeFromTemplate(TemplateLoader loader) {
			rect1.setTop(30);
			rect3.setTop(30);
		}
		
		@TemplateCallback
		public void rectClicked(TemplateView sender, MouseEvent event) {
			sender.setTop(sender.getTop() + 10);
		}
		
		@TemplateCallback
		public void rect3pressed(TemplateView sender, MouseEvent event) {
			
		}
		
		@TemplateCallback
		public void rect3dragged(TemplateView sender, MouseEvent event) {
			sender.setPosition(event.getPosition(sender.getSuperView()));
		}
		
		@TemplateCallback
		public void rect3released(TemplateView sender, MouseEvent event) {
		}
		
	}

}
